package com.itqian.controller.seller;

import com.itqian.common.exception.Assert;
import com.itqian.common.exception.ResponseEnum;
import com.itqian.common.utils.ResultVOUtil;
import com.itqian.common.vo.ResultVO;
import com.itqian.seller.entity.FoodCategory;
import com.itqian.seller.service.FoodCategoryService;
import com.itqian.seller.vo.FoodCategoryVO;
import com.itqian.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @author shishuqian
 * date 2022/3/21
 * time 22:05
 **/
@RestController
@RequestMapping("/seller/foodCategory")
public class FoodCategoryController {

    @Autowired
    private FoodCategoryService foodCategoryService;

    /**
     *  查询店铺商品的所有分类
     */
    @GetMapping("/findAll")
    public ResultVO findAllFoodCategory(@RequestHeader("token") String token){
        //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);

        List<FoodCategoryVO> result = foodCategoryService.findAllCategoryVO(storeId);
        return ResultVOUtil.success(result);
    }

    /**
     *  添加或修改店铺菜品分类名
     */
    @PostMapping("/saveOrUpdate")
    public ResultVO saveOrUpdate(@RequestHeader("token") String token,@Valid @RequestBody FoodCategoryVO vo){
        //获取店铺id
        Integer storeId = TokenUtils.getStoreId(token);

        FoodCategory foodCategory = new FoodCategory();
        foodCategory.setCategoryId(vo.getCategoryId())
                .setStoreId(storeId)
                .setCategoryName(vo.getCategoryName());

        boolean b = foodCategoryService.saveOrUpdate(foodCategory);
        if (!b){
            return ResultVOUtil.fail("操作失败，请重试...");
        }
        return ResultVOUtil.success(null);
    }

    /**
     *  删除店铺菜品分类
     */
    @DeleteMapping("/delete/{categoryId}")
    public ResultVO delete(@PathVariable Integer categoryId){
        //参数非空校验
        Assert.notNull(categoryId, ResponseEnum.PARAM_NOTNULL);

        boolean b = foodCategoryService.removeById(categoryId);
        if (!b){
            return ResultVOUtil.fail("删除失败，请重试...");
        }
        return ResultVOUtil.success(null);
    }


}
